<?php
namespace PsiFramework\Classes\Factory;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

use \PsiFramework\Classes\Util\Application;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\Manager\ParametersManager;

/**
 * The ActionFactory is used to get the correct Service instance
 *
 * @package Factories
 */
class ServiceFactory
{

    protected static /*ServiceFactory*/ $_singleton = null;

    protected /*Application*/ $_application ;

    protected function __construct()
    {
        $paramManager = ParametersManager::getInstance();
        $this->_application =
            ApplicationManager::getInstance()->getApplication();
    }

    /**
     * Static function to retrieve a new instance of
     * the service based on its name
     *
     * @param     string             $serviceName    the service name
     * @return     AbstractService    $service        the wanted AbstractService
     */
    public function getService(/*string*/ $serviceName)
    {
        $serviceClass = $this->_application->getServiceClass($serviceName);
        import($serviceClass);
        $serviceName = getClassName($serviceClass);
        return new $serviceName();
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return ServiceFactory instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new ServiceFactory();
        }

        return self::$_singleton;
    }
}
